<?php

namespace html;

class tag extends \html\core\tag
{

	/**
	 * Create basic tag. Kind of constructor after dispatching an object from it self
	 * @param string $tagname
	 * @param string $tagending
	 * @return boolean
	 */
	function createTag($tagname, $tagending)
	{
		$tagname = trim($tagname);
		if( $tagname === "" )
		{
			throw new Exception("Tagname with ID ".$this->getId()." of length 0");
		}

		//Inject the manager
		$this->setAttributes(new core\attributes());
		$this->setStyles(new core\styles());

		if( $tagname[0] === "/" )
		{
			$this->setType(TAG_CLOSE);
			$this->setTagname(substr($tagname, 1));
			return true;
		}
		else
		{
			$tagending = trim($tagending);
			if( $tagending === "/" )
			{
				$this->setType(TAG_SINGLE);
				$this->setTagname($tagname);
			}
			else
			{
				$this->setType(TAG_OPEN);
			}
			$this->setTagname($tagname);

			return true;
		}
		throw new Exception("Error creating tagname!");
	}

	function attributeManager()
	{
		return $this->getAttributes();
	}

	function styleManager()
	{
		return $this->getStyles();
	}

	public function reLevel($by=-1)
	{
		$currentLevel = $this->getLevel();

		if( ($currentLevel + $by) < 1 )
		{
			$newLevel = 1;
		}
		else
		{
			$newLevel = $currentLevel + $by;
		}

		$this->setLevel($newLevel);
	}

	public function sanitizeAttributes($referenceAttributes)
	{
		if( $referenceAttributes && count($this->getAttributes()) )
		{
			$validAttributes = array_flip($referenceAttributes);

			if( $this->attributeManager()->getAttributes() ) foreach( $this->attributeManager()->getAttributes() as $id => $attribute )
				{
					if( !isset($validAttributes[strtolower($attribute->getKey())]) )
					{
						$this->attributeManager()->removeAttribute($attribute->getKey());
						$this->addNotice("Invalid attribute [".$attribute->getKey()."] has been removed!");
					}
				}
		}
	}

	public function html($isSub=false)
	{
		if( $this->isOpenTag() )
		{
			$out.= $this->getTag().$this->getText();
		}

		if( $this->hasChildren() )
		{
			foreach( $this->getChildren() as $childTag )
			{
				$str.= $childTag->html(true);
			}
		}
		$out.= $str;

		if( !$isSub && $this->hasCloseTag() )
		{
			$out.= $this->getCloseTag()->getTag();
		}
		elseif( $isSub && $this->isSingleTag() )
		{
			$out.= $this->getTag().$this->getText();
		}
		elseif( $isSub && $this->hasCloseTag() )
		{
			$out.= $this->getCloseTag()->getTag().$this->getCloseTag()->getText();
		}

		return $out;
	}

	public function getText()
	{
//		return parent::getText();
		$text = trim(parent::getText(),"\n");
		return html_entity_decode($text, ENT_COMPAT, "UTF-8");
	}

	public function getTag()
	{
//		if( $this->getTagname() == "a" )
//		{
//			$this->setTagname("xm:test");
//		}
		$tag = parent::getTag();

//		if( $this->hasDependancy() && !$this->isCloseTag() )
//		{
//			$tag.= "(<-Belongs to ".$this->getDependancy().")";
//		}

		if( ($this->isCloseTag() || $this->isSingleTag()) && $this->getBlocktype() == "block" )
		{
			$tag.="\n";
		}
		return $tag;
	}

}

?>
